/*	kd:
	
	You can set your favourite shop. If you set a favorite, it'll open on that
	one every time, else it'll open where last closed. This handles the
	animation triggers, too... just pretend this is on the main level, but I
	wanted to hide everything.
	
	*/

struct Gc_Shop_Favorite {
	private int						shop_count;
	
	private int						favorite_index;
	private bool					has_favorite;
	
	private Gc_DefaultCvar			cvar_shop_index;
	private Gc_DefaultCvar			cvar_shop_return;
	
	// kd: Animation stuff (not private on purpose).
	Gc_Spart_Favorite				fx_gen;
	
	void OnInit (
	Gcs_ShopList			shop_list,
	Gc_ScreenParticles		particle_fx,
	Gc_Shop_ButtonDrawing	button_anim,
	vector2					sprite_to_window) {
		shop_count		= shop_list.ShopCount();
		
		cvar_shop_index.Find("gc_shop_fav");
		cvar_shop_return.Find("gc_shop_usefav");
		
		// kd: Put some sauce on it.
			
		// kd: The particle generators and button sprites live in different
		// spaces, but here we have a bus between worlds.
		let origin = button_anim.move_anim.FinalPos(
			Gc_Shop_UiHandling.favorite_on);
		
		origin.x *= sprite_to_window.x;
		origin.y *= sprite_to_window.y;
		
		if(fx_gen) {
			fx_gen.SetOrigin(origin);
			return;
		}
		
		fx_gen = Gc_Spart_Favorite(new("Gc_Spart_Favorite"));
		
		if(fx_gen) {
			fx_gen.SetOrigin(origin);
			fx_gen.Trigger();
			particle_fx.Add(fx_gen);
		}
	}
	
	int OpeningShopIndex () const {
		favorite_index		= clamp(
			cvar_shop_index.GetInt(def_val: 0), 0, shop_count - 1);
		has_favorite		= cvar_shop_return.GetBool(def_val: false);
		return favorite_index;
	}
	
	void Save (int current_shop_index) {
		cvar_shop_index.SetInt(has_favorite ? favorite_index : current_shop_index);
		cvar_shop_return.SetBool(has_favorite);
	}
	
	void Set (int current_shop_index) {
		if(current_shop_index == favorite_index) {
			has_favorite = !has_favorite;
		}
		
		else {
			has_favorite = true;
		}
		
		favorite_index = current_shop_index;
	}
	
	bool IsFavorite (int current_shop_index) const {
		return has_favorite && current_shop_index == favorite_index;
	}
}
